package com.wsl.utils;

import com.thoughtworks.xstream.XStream;
import com.wsl.Entity.People;
import com.wsl.Entity.Peoples;


public class XmlUtil {

    static final Class[] trustClass = new Class[]{
            Peoples.class,
            People.class
    };

    private static XStream xStream;

    private synchronized static void init() {

        if (xStream != null) {
            return;
        }
        xStream = new XStream();
        XStream.setupDefaultSecurity(xStream);
        xStream.processAnnotations(trustClass);
        xStream.allowTypes(trustClass);

    }

    public static <T> T getBean(String xml, Class<T> tClass) {
        if (xStream == null) {
            init();
        }
        return (T) xStream.fromXML(xml);
    }

    public static String getString(Object xmlObj) {
        if (xmlObj == null) {
            return null;
        }
        if (xStream == null) {
            init();
        }
        return xStream.toXML(xmlObj);
    }

}
